package com.huawei.leetcode;

/**
 * @Author: liu.cheng.su
 * @Time: 2023/12/13 0013 19:58
 * @Description:
 */
public class Code2697 {
    public static void main(String[] args) {
        Code2697 code2697 = new Code2697();
        System.out.println(code2697.makeSmallestPalindrome("qzerewq"));
    }

    public String makeSmallestPalindrome(String s) {
        char[] s_chars = s.toCharArray();
        for (int i = 0; i < s_chars.length; i++) {
            int j = s.length() - 1 - i;
            if (i >= j) {
                break;
            }

            if(s_chars[i] !=s_chars[j]){
                if(s_chars[i]>s_chars[j]){
                    s_chars[i] = s_chars[j];
                }else {
                    s_chars[j] = s_chars[i];
                }
            }
        }
        return new String(s_chars);
    }
    public String makeSmallestPalindrome1(String s) {
        for (int i = 0; i < s.length(); i++) {
            int j = s.length() - 1 - i;
            if (i >= j) {
                break;
            }
            char c_i = s.charAt(i);
            char c_j = s.charAt(j);
            if (c_i != c_j) {
                if (c_i > c_j) {
                    s = replace(s,i,c_j);
                } else {
                    s = replace(s,j,c_i);
                }
            }
        }
        return s;
    }

    private String replace(String s, int index, char c) {
        char[] chars = s.toCharArray();
        chars[index] = c;
        return new String(chars);
    }

    public String makeSmallestPalindrome2(String s) {
        for (int i = 0; i < s.length(); i++) {
            int j = s.length() - 1 - i;
            if (i >= j) {
                break;
            }
            char c_i = s.charAt(i);
            char c_j = s.charAt(j);
            if (c_i != c_j) {
                if (c_i > c_j) {
                    s = s.substring(0, i) + String.valueOf(c_j) + s.substring(i + 1);
                } else {
                    s = s.substring(0, j) + String.valueOf(c_i) + s.substring(j + 1);
                }
            }
        }
        return s;
    }
}
